#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web.Categories.Management.Forms
{
    public partial class ApproveLeaveForm : Form
    {
        private EmployeeVacation _employeeVacation;
        private bool _canUpdate;
        public ApproveLeaveForm(EmployeeVacation employeeVacation, bool CanUpdate)
        {
            InitializeComponent();
            _employeeVacation = employeeVacation;
            _canUpdate = CanUpdate;
            InitialData();
        }

        private void InitialData()
        {
            lblEmployeeName.Text = LanLayer.GetTrans(122) + ":";
            lblFrom.Text = LanLayer.GetTrans(129) + ":";
            lblLength.Text = LanLayer.GetTrans(127) + ":";
            lblNote.Text = LanLayer.GetTrans(134) + ":";
            lblOperateNote.Text = LanLayer.GetTrans(149) + ":";
            lblTo.Text = LanLayer.GetTrans(130) + ":";
            lblVacationType.Text = LanLayer.GetTrans(123) + ":";
            lblFinalBalance.Text = LanLayer.GetTrans(194) + ":"; 

            btnApprove.Text = LanLayer.GetTrans(150);
            btnDeny.Text = LanLayer.GetTrans(151);


            if (_employeeVacation.Status == "Pending" && _canUpdate)
            {
                btnApprove.Enabled = true;
                btnDeny.Enabled = true;
            }
            else
            {
                btnApprove.Enabled = false;
                btnDeny.Enabled = false;
            }

            tbEmployeeName.Text = _employeeVacation.Employee.EnglishName + "(" + _employeeVacation.Employee.ChineseName + ")";
            tbVacationType.Text = LanLayer.GetTrans(_employeeVacation.VacationType_lkp.Content_id.Value);
            tbFrom.Text = _employeeVacation.FromTime.HasValue ? _employeeVacation.FromTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
            tbTo.Text = _employeeVacation.EndTime.HasValue ? _employeeVacation.EndTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
            tbLength.Text = _employeeVacation.Length.HasValue ? Utility.ConvertLengthToString(_employeeVacation.Length.Value) : "";

            tbNote.Text = _employeeVacation.Note;

            decimal finalHours = 0;
            var vacationbase = Global.Context.VacationBases.Where(o => o.Employee.EmployeeID == _employeeVacation.EmployeeID && o.IsDeleted == false && o.IsCurrent && o.VacationTypeCode == "ANN").FirstOrDefault();
            if (vacationbase != null)
            {
                finalHours = vacationbase.FinalHours.HasValue ? vacationbase.FinalHours.Value : 0;
            }

            txtFinalBalance.Text = Utility.ConvertLengthToString(finalHours);

            tbOperateNote.Text = _employeeVacation.OperatorNote;
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            var vacationBase = Global.Context.VacationBases.FirstOrDefault(o => o.EmployeeID == _employeeVacation.EmployeeID && o.VacationTypeCode == _employeeVacation.VacationTypeCode && o.IsCurrent);

            if (vacationBase != null && vacationBase.FinalHours < (_employeeVacation.Length.HasValue ? _employeeVacation.Length.Value : 0))
            {
                MessageBox.Show("There is no enough balance for this employee!");
                return;
            }

            _employeeVacation.Status = "Approved";
            _employeeVacation.UpdateBy = Global.CurrentUser.EmployeeID;
            _employeeVacation.UpdateTime = DateTime.Now;
            _employeeVacation.OperateTime = DateTime.Now;
            _employeeVacation.OperatorID = Global.CurrentUser.EmployeeID;
            _employeeVacation.OperatorNote = tbOperateNote.Text;


            //Handle VacationBase
            Employee employee = _employeeVacation.Employee;
            
            
            if (vacationBase != null)
            {
                vacationBase.FinalHours = vacationBase.FinalHours - (_employeeVacation.Length.HasValue ? _employeeVacation.Length.Value : 0);
            }

            if (_employeeVacation.VacationTypeCode == "TIL")
            {
                vacationBase = Global.Context.VacationBases.FirstOrDefault(o => o.EmployeeID == _employeeVacation.EmployeeID && o.VacationTypeCode == "ANN" && o.IsCurrent);
                if (vacationBase != null)
                {
                    vacationBase.FinalHours = vacationBase.FinalHours + (_employeeVacation.Length.HasValue ? _employeeVacation.Length.Value : 0);
                }
            }

            Global.Context.SaveChanges();

            Close();
        }

        private void btnDeny_Click(object sender, EventArgs e)
        {
            _employeeVacation.Status = "Denied";
            _employeeVacation.UpdateBy = Global.CurrentUser.EmployeeID;
            _employeeVacation.UpdateTime = DateTime.Now;
            _employeeVacation.OperateTime = DateTime.Now;
            _employeeVacation.OperatorID = Global.CurrentUser.EmployeeID;
            _employeeVacation.OperatorNote = tbOperateNote.Text;
            Global.Context.SaveChanges();
            Close();
        }
    }
}